package com.fly.common.persistence;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fly.common.utils.Reflections;
import com.sun.istack.internal.NotNull;

/**
 * Created by SWan on 2017/4/26.
 */
public abstract class TreeEntity<T> extends DataEntity<T> {

    protected T parent;	// 父级
    protected String parentIds; // 所有父级id
    protected String name; 	// 名称
    protected Integer sort;		// 排序

    public TreeEntity() {
        this.sort = 30;
    }

    public TreeEntity(Integer id) {
        super(id);
    }

    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     * @return
     */
    /**
     * jackson中的@JsonBackReference和@JsonManagedReference，以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归（infinite recursion）问题。这些标注均可用在属性或对应的get、set方法中
     * @return
     */
    @JsonBackReference
    @NotNull
    public abstract T getParent();

    /**
     * 父对象，只能通过子类实现，父类实现mybatis无法读取
     * @return
     */
    public abstract void setParent(T parent);


    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Integer getParentId(){
        Integer id = null;
        if (parent != null){
            System.out.println("parent");
            id = (Integer) Reflections.getFieldValue(parent, "id");
        }
        System.out.println("id =" + id );
        return id!=null ? id : -1;
    }
}
